/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var weireCookies = {};
/**//**
 * 设置Cookies
 */
weireCookies.set = function(name, value){
     var argv = arguments;
     var argc = arguments.length;
     var expires = (argc > 2) ? argv[2] : null;
     var path = (argc > 3) ? argv[3] : '/';
     var domain = (argc > 4) ? argv[4] : null;
     var secure = (argc > 5) ? argv[5] : false;
     document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
};
/**//**
 * 读取Cookies
 */
weireCookies.get = function(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while(i < clen){
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return weireCookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if(i == 0)
            break;
    }
    return null;
};
/**//**
 * 清除Cookies
 */
weireCookies.clear = function(name) {
  if(weireCookies.get(name)){
    var expdate = new Date();
    expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
    weireCookies.set(name, "", expdate);
  }
};

weireCookies.getCookieVal = function(offset){
   var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1){
       endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
};

function weireAuth(weiresearch_oauth_id, weiresearch_oauth_secret, grant_type, publisher_id) {
    //微瑞授权账号
    this.weiresearch_oauth_id = weiresearch_oauth_id;
    //微瑞账号密码
    this.weiresearch_oauth_secret = weiresearch_oauth_secret;
    //类型
    this.grant_type = grant_type;
    //sina uid
    this.publisher_id = publisher_id;

    //微瑞OAuth验证
    this.authorize = function (callback){
        $.ajax({
            type: 'GET',
            dataType: "jsonp",
            success: function(data) {
                if(weireCookies.get("sinawb_userID")){
                    $.ajax({
                        type: 'GET',
                        dataType: 'jsonp',
                        success: function(){},
                        url: "http://202.85.221.51:30080/Interestgraph/tag?vpuid=1155&token="+data.publisher_token+"&uid="+weireCookies.get("sinawb_userID")
                    });
                }
                callback(data.oauth_token);
            },
            url: 'http://zhamengzi.com:40080/weiresearch/access_token?weiresearch_oauth_id='+this.weiresearch_oauth_id+'&weiresearch_oauth_secret='+this.weiresearch_oauth_secret+'&grant_type='+this.grant_type+'&publisher='+this.publisher_id
        });
    };

    //返回新浪UID
    this.sinaUid = function (config){
        $.ajax({
            type: 'GET',
            dataType: "jsonp",
            success: function(data) {
                if(config.callback){
                    $.ajax({
                        type: 'GET',
                        dataType: 'jsonp',
                        success: function(){},
                        url: "http://202.85.221.51:30080/Interestgraph/tag?vpuid=1155&token="+data.token+"&uid="+data.id
                    });
                    config.callback(data);
                }
            },
            url: 'http://zhamengzi.com:40080/weiresearch/resources/netizen?publisher='+this.publisher_id+'&weiresearch_oauth_token='+config.access_token+'&nickname='+encodeURI(config.nickname)
        });
    }

    //添加微瑞schedule项目
    this.addSchedule = function (config){
        try{
            var publishContent = config.content;
            if(publishContent.charAt(0) == '@'){
                var contentBlock = publishContent.split(' ');
                var beAt = contentBlock[0];
                console.log(beAt);
                if(beAt == '@'){
                    return;
                }
                beAt = beAt.substring(1);
                console.log(beAt);
                var doAdd = "(function(data) {\n"+
                                "if(data&&data!=null&&data!='null'){\n"+
                                "$.ajax({\n"+
                                    "type: 'GET',\n"+
                                    "dataType: 'jsonp',\n"+
                                    "url: 'http://zhamengzi.com:40080/weiresearch/resources/schedule?publisher="+this.publisher_id+"&weiresearch_oauth_token="+config.access_token+"&content="+config.content+"&vpuid=1155&estimatetime="+(new Date()).getTime()+"'\n"+
                                "});\n"+
                                "}\n"+
                            "})";
                console.log(doAdd);
                var doAddCallback = eval(doAdd);
                console.log(typeof doAddCallback);
                $.ajax({
                    url: 'http://zhamengzi.com:40080/weiresearch/resources/netizen?publisher='+this.publisher_id+'&weiresearch_oauth_token='+config.access_token+'&nickname='+encodeURI(beAt),
                    type: "GET",
                    dataType: "jsonp",
                    success: doAddCallback
                });
                doAddCallback('ad');
            }
            $.ajax({
                type: 'GET',
                dataType: "jsonp",
                success: function(data) {
                    config.callback(data);
                },
                url: 'http://zhamengzi.com:40080/weiresearch/resources/schedule?publisher='+this.publisher_id+'&weiresearch_oauth_token='+config.access_token+'&content='+config.content+'&vpuid=1155&estimatetime='+(new Date()).getTime()
            });
        }catch(err){

        }
    }
}